#!/usr/bin/python3
# -*- coding:UTF-8 -*-
#作品名称：冰枫蓝叶聊天女仆 - 错误记录模块
#作者：冰枫火灵X
#本模块许可：GNU LGPL v3 or later

import libmaidtimer
import sys
import os
    
def maid_error(etitle,etext,etype):
    if not os.access('./maidcfg',os.F_OK):
        os.mkdir('./maidcfg')
    if not os.access('./maidcfg/error.txt',os.F_OK):
        erfile=open('./maidcfg/error.txt','w',encoding='UTF-16')
        erfile.write('#此文件是冰枫女仆的报错信息哦！\n')
        erfile.close()
    with open('./maidcfg/error.txt','a+',encoding='UTF-16') as errorinfo:
        #print(etype+str(type(etype)))
        errorinfo.write('\n===')
        errorinfo.write('\n错误发生时间：{:s}'.format(libmaidtimer.fulltt()))
        errorinfo.write('\n错误标题：{leixing}'.format(leixing=str(etitle)))
        errorinfo.write('\n错误内容：{neirong}'.format(neirong=str(etext)))
        errorinfo.write('\n调试标志：{debugx}'.format(debugx=bool(int(etype))))
        errorinfo.flush()
        errorinfo.close()
    return 0
        
if __name__ == '__main__':
    try:
        error_title=sys.argv[1]
        error_text=sys.argv[2]
        error_type=sys.argv[3]
        maid_error(error_title,error_text,error_type)
    except IndexError as ie:
        print('您好，请按照如下格式指定参数：\n')
        print('libmaiderror.py 错误标题字符串 错误内容字符串 调试标志（0或1）')
        print(ie)
        sys.exit(1)
    else:
        print('内容已经写入error.txt文件，编码方式：UTF-16')
        sys.exit(0)
    finally:
        pass
